// import '../themes';

import React from 'react';
import { Route, withRouter, Redirect, Switch } from 'react-router-dom';
import { connect } from 'react-redux';
import { Spin } from 'antd';
import { getAuth } from 'src/utils/auth';
import * as UserAction from 'actions/UserAction';

import Login from 'src/container/login';
import Frame from 'src/container/frame';

class App extends React.Component {
  componentDidMount() {
    // 验证登陆
    const info = getAuth();
    if (info) {
      this.props.dispatch(UserAction.saveAuth(info));
    } else {
      // 清空用户信息，重定向登陆
      this.props.dispatch(UserAction.clearAuth());
    }
  }

  render() {
    const { loading, info, location } = this.props;
    const userInfo = info || {};
    return (
      <Spin spinning={ loading }>
        { (location.pathname !== '/login' && !userInfo.username) && <Redirect to='/login' /> }
        <Switch>
          <Route exact component={ Login } path='/login' />
          <Route path='/' component={ Frame } />
        </Switch>
      </Spin>
    );
  }
}

function mapStateToProps(state) {
  return {
    loading: state.loading,
    info: state.user.info,
  };
}

export default withRouter(connect(mapStateToProps)(App));